/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.cbcgateway.smscbresponse;

import com.elcom.emocbc.dbmapping.tables.pojos.Cell;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author GiangLT
 */
public class GetBscCellsResponseParser {

    private static final Logger logger = LoggerFactory.getLogger(GetBscCellsResponseParser.class);

    public static List<Cell> parse(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilder dBuilder = null;
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            dBuilder = dbFactory.newDocumentBuilder();

            Document doc = dBuilder.parse(inputStream);
            doc.getDocumentElement().normalize();

            NodeList cellNodes = doc.getElementsByTagName("cell");
            List<Cell> cells = new ArrayList<Cell>(cellNodes.getLength());
            for (int i = 0; i < cellNodes.getLength(); i++) {
                Element cellNode = (Element) cellNodes.item(i);
                String bscId = cellNode.getElementsByTagName("bscId").item(0).getTextContent();
                String ci = cellNode.getElementsByTagName("ci").item(0).getTextContent();
                String gatewayId = cellNode.getElementsByTagName("gatewayId").item(0).getTextContent();
                String id = cellNode.getElementsByTagName("id").item(0).getTextContent();
                String lac = cellNode.getElementsByTagName("lac").item(0).getTextContent();
                String mcc = cellNode.getElementsByTagName("mcc").item(0).getTextContent();
                String mnc = cellNode.getElementsByTagName("mnc").item(0).getTextContent();
                String name = cellNode.getElementsByTagName("name").item(0).getTextContent();
                String status = cellNode.getElementsByTagName("status").item(0).getTextContent();
                String statusDescription = cellNode.getElementsByTagName("statusDescription").item(0).getTextContent();

                Cell cell = new Cell();
                cell.setBscId(bscId);
                cell.setCellId(ci);
                cell.setGatewayId(gatewayId);
                cell.setCellKey(id);
                cell.setLac(lac);
                cell.setMcc(mcc);
                cell.setMnc(mnc);
                cell.setCellName(name);
                cell.setStatus(status);
                cell.setDescription(statusDescription);

                cells.add(cell);
            }
            return cells;
        } finally {
            if (dBuilder != null) {
                dBuilder.reset();
            }
            IOUtils.closeQuietly(inputStream);
        }
    }
}
